package com.chat.service.impl;

import java.util.HashMap;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chat.dao.UserMapper;
import com.chat.domain.User;
import com.chat.param.JsonContent;
import com.chat.param.LoginParam;
import com.chat.service.UserService;

@Transactional
@Service
public class UserServiceImpl implements UserService {
	@Resource
	private UserMapper userDao;
	@Autowired
	private HttpSession session;

	@Autowired
	private HttpServletRequest request;

	@Override
	public JsonContent canUserLogin(User userParam) {
		JsonContent jsonContent = new JsonContent();
		String usernameParam = userParam.getUsername();
		String passwordParam = userParam.getPassword();
		User existUser = userDao.selectByUserName(usernameParam);
		if (existUser != null) {
			String existPassword = existUser.getPassword();
			
			if (existPassword.equals(passwordParam)) {
				HttpSession session = request.getSession();
				setSessionData(session, existUser);
				return jsonContent;
			} else {
				jsonContent = jsonContent.getUserPasswordError();
			}
		} else {
			jsonContent = jsonContent.getUserNotExist();
		}
		return jsonContent;
	}

	private void setSessionData(HttpSession session, User existUser) {
		String username = existUser.getUsername();
		session.setAttribute("LOGIN_USERNAME", username);
	}

}